package com.max.xihu.patterns.proto;

import java.util.ArrayList;

/**
 * @Author: Max
 * @Date: 2020-09-09 08:20
 * @Description:浅拷贝示例
 */
public class ShallowCopy implements Cloneable {

    private ArrayList<String> list = new ArrayList<>();

    @Override
    protected ShallowCopy clone() {
        ShallowCopy shallowCopy = null;
        try {
            shallowCopy = (ShallowCopy) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return shallowCopy;
    }

    public ArrayList<String> getList() {
        return this.list;
    }

    public void setList(String value) {
        this.list.add(value);
    }

    public static void main(String[] args) {
        ShallowCopy shallowCopy = new ShallowCopy();
        shallowCopy.setList("张三");

        ShallowCopy clone = shallowCopy.clone();
        clone.setList("李四");
        System.out.println(shallowCopy.getList());
    }
}
